#include <REG52.H>
#include "onewire.h"


unsigned int Temp;

code unsigned char SMG1[20] = {0xc0,0xf9,0xa4,0xb0, 0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e };

code unsigned char code SMG2[20] = {0xc0, 0x79, 0x24, 0x30, 0x19, 0x12, 0x02, 0x78, 0x00, 0x10, 0x03, 0x46, 0x21, 0x06,0x0e};

void Delay1ms(unsigned int t)	//@11.0592MHz
{
	unsigned char data i, j;

	while(t--)
	{
		i = 2;
		j = 199;
	do
	{
		while (--j);
		
	} while (--i);
}
}

void Delay(unsigned int t)
{
	while(t--);
}

void SelectHC573(unsigned char channel)
{
	switch (channel)
	{
		case 4:
			P2 = (P2 & 0x1f) | 0x80;
			break;
		case 5:
			P2 = (P2 & 0x1f) | 0xa0;
			break;	
		case 6:
			P2 = (P2 & 0x1f) | 0xc0;
			break;
		case 7:
			P2 = (P2 & 0x1f) | 0xe0;
			break;
		case 0:
			P2 = 0xff;
			break;
	}
}

void SMG_Bit(unsigned char pos,unsigned char dat)
{
	SelectHC573(6);
	P0 = 0x01 << pos;
	SelectHC573(7);
	P0 = dat;
	SelectHC573(0);
}

void Init_SMG(unsigned char dat)
{

	SelectHC573(6);
	P0 = 0xff;
	SelectHC573(7);
	P0 = dat;
	SelectHC573(0);
}

void SHOWSMG()
{
	SMG_Bit(7,SMG1[Temp%10]);
	Delay(100);
	SMG_Bit(6,SMG1[(Temp%100)/10]);
	Delay(100);	
	SMG_Bit(5,SMG1[(Temp/100)%10]);
	Delay(100);	
	

	
	Init_SMG(0xff);
	Delay(100);
}

void Delay1(unsigned int t)
{
	while(t--)
	{
		SHOWSMG();
	}
}

void DS18b20()
{
	unsigned char LSB,HSB;
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
	Delay1(1000);
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0xbe);
	LSB = Read_DS18B20();
	HSB = Read_DS18B20();
	
	Temp = HSB;
	Temp = (Temp<<8) | LSB;
	if((Temp & 0xf800) == 0x0000)
	{
		Temp >>= 4;
		Temp *= 10;
		Temp = Temp + (LSB & 0x0f)*0.625;
	}
}

void main()
{
	
	while(1)
	{
		DS18b20();
		SHOWSMG();
	}
	
}










